package com.leetcode;

import java.util.Scanner;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No05
 * @description 5. 最长回文子串
 * @date 2021/3/14 下午5:52
 */
public class No05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String res = longestPalindrome(s);
        System.out.println(res);

    }

    public static String longestPalindrome(String s) {
        if(s == null || s.length() == 0){
            return "";
        }
        int start = 0,end = 0,len = s.length();
        for (int i = 0; i < len; i++) {
            int len1 = lengthOfPalindrome(s,i,i);
            int len2 = lengthOfPalindrome(s,i,i+1);
            int length = Math.max(len1,len2);
            if(length > end - start){
                start = i - (length - 1) / 2;
                end = i + length / 2;
            }
        }
        return s.substring(start,end + 1);

    }

    private static int lengthOfPalindrome(String s,int left,int right){
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
            left--;
            right++;
        }
        return right - left - 1;
    }

}
